<?php


namespace app\api\controller;


use app\admin\model\StaffOrder;
use think\Controller;
use think\Request;

class StaffServices extends Controller
{
    public function staffServicesList(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $list = [];
        //人员列表
        $staffServices = new \app\admin\model\StaffServices();
        $where['status'] = 1;
        $rs['staff'] = $staffServices->getAll($where);

        //当前用户预约服务人员
        $staffOrder = new StaffOrder();
        $orderWhere['user_id'] = $data['user_id'];
        $staffArray = $staffOrder->getAll($orderWhere);

        //获取已预约做标记
        foreach($rs['staff'] as $k){
            foreach($staffArray as $l){
                if($k['id'] == $l['staff_id']){
                    $k['button'] = $l['status'];
                }
            }
            if(!isset($k['button']))$k['button'] = '';

            switch($k['role']){
                case 0:
                    $list['ch'][] = $k;
                break;
                case 1:
                    $list['sj'][] = $k;
                    break;
                case 2:
                    $list['mx'][] = $k;
                    break;
                case 3:
                    $list['jb'][] = $k;
                    break;
            }
        }

        if($list){
            $res['data']['code'] = 1;
            $res['data']['msg'] = '获取成功';
            $res['data']['data'] = $list;
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '获取失败';
        }
        $res['status'] = 200;
        echo json_encode($res);die;
    }

    public function addStaffServices(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or !isset($data['staff_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $staffOrder = new StaffOrder();
        $getWhere['user_id'] = $data['user_id'];
        $getWhere['staff_id'] = $data['staff_id'];
        $getWhere['status'] = 0;
        $isIn = $staffOrder->getOne($getWhere);
        if($isIn){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '已提交预约申请';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        $addData['user_id'] = $data['user_id'];
        $addData['staff_id'] = $data['staff_id'];
        $addData['create_time'] = time();
        $rs = $staffOrder->add($addData);
        if($rs){
            $res['data']['code'] = 1;
            $res['data']['msg'] = '添加购物车成功';
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '添加购物车失败';
        }
        $res['status'] = 200;
        echo json_encode($res);die;
    }
}